/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dspot.declex.actions;

import ohos.agp.components.DatePicker;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import com.dspot.declex.actions.component.DatePickerDialog;
import com.dspot.declex.annotation.action.ActionFor;
import com.dspot.declex.annotation.action.Assignable;
import com.dspot.declex.annotation.action.FormattedExpression;
import com.dspot.declex.annotation.action.StopOn;

import org.ohosannotations.annotations.RootContext;

import java.util.Calendar;

/**
 * 日期对话框行动持有人
 *
 * @author Administrator
 * @since 2021-07-16
 */
@ActionFor("DateDialog")
public class DateDialogActionHolder {

    /**
     * 对话框
     */
    DatePickerDialog dialog;

    /**
     * 上下文
     */
    @RootContext
    Context context;

    /**
     * 日期设置
     */
    private DateSetRunnable DateSet;

    /**
     * 初始化
     */
    void init() {
        init(null);
    }

    /**
     * 初始化
     *
     * @param calendar A Calendar object containing the initial day, month and year
     * for the date picker.
     */
    void init(Calendar calendar) {

        if (calendar == null) {
            // Use the current time as the default values for the picker
            calendar = Calendar.getInstance();
        }

        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
        final int day = calendar.get(Calendar.DATE);

        init(year, month, day);
    }

    /**
     * 初始化
     *
     * @param year The initial year of the dialog.
     * @param month The initial month of the dialog.
     * @param day The initial day of the dialog.
     */
    void init(int year, int month, int day) {

        // Create a new instance of DatePickerDialog
        dialog = new DatePickerDialog(
            context,
            new DatePicker.ValueChangedListener() {
                @Override
                public void onValueChanged(DatePicker datePicker, int year, int month, int day) {
                    if (DateSet != null) {
                        DateSet.onValueChanged(datePicker, year, month, day);
                    }
                }
            },
            year, month, day
        );

    }


    /**
     * 构建
     *
     * @param DateSet <i><b>(default)</b></i> This Action Selector will be executed when the Date is set.
     * <br><br>Parameters
     * <ul>
     * <li>datePicker - The DatePicker View associated with this listener.</li>
     * <li>year - The year that was set.</li>
     * <li>month - The month that was set (0-11) for compatibility with {@link Calendar}.</li>
     * <li>day - The day of the month that was set.</li>
     * </ul>
     * @param Canceled This Action Selector will be invoked when the dialog is canceled.
     * @param Dismissed This Action Selector will be invoked when the dialog is dismissed.
     */
    void build(
        final DateSetRunnable DateSet,
        final Runnable Canceled,
        final Runnable Dismissed) {

        this.DateSet = DateSet;

        if (Canceled != null) {
            dialog.setDestroyedListener(new CommonDialog.DestroyedListener() {

                @Override
                public void onDestroy() {
                    Canceled.run();
                }
            });
        }

        if (Dismissed != null) {
            dialog.setDestroyedListener(new CommonDialog.DestroyedListener() {

                @Override
                public void onDestroy() {
                    Dismissed.run();
                }
            });
        }

    };

    /**
     * 执行
     */
    void execute() {
        dialog.show();
    }

    /**
     * 对话框
     *
     * @return Internal Dialog instance to make further configurations.
     */
    @StopOn("show")
    public DatePickerDialog dialog() {
        return this.dialog;
    }

    /**
     * 对话框
     * Assigns the Internal Dialog instance.
     *
     * @param dialog The variable to which the dialog is going to be assigned
     * @return {@link DateDialogActionHolder}
     */
    public DateDialogActionHolder dialog(@Assignable("dialog") DatePickerDialog dialog) {
        return this;
    }

    /**
     * 标题
     *
     * @param title 标题
     * @return {@link DateDialogActionHolder}
     */
    public DateDialogActionHolder title(@FormattedExpression String title) {
        dialog.setTitleText(title);
        return this;
    }

    /**
     * 消息
     *
     * @param message 消息
     * @return {@link DateDialogActionHolder}
     */
    public DateDialogActionHolder message(@FormattedExpression String message) {
        dialog.setContentText(message);
        return this;
    }

    public static abstract class DateSetRunnable implements Runnable, DatePicker.ValueChangedListener {

        public DatePicker datePicker;
        public int year;
        public int month;
        public int day;

        @Override
        public void onValueChanged(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
            this.datePicker = datePicker;
            this.year = year;
            this.month = monthOfYear;
            this.day = dayOfMonth;
            run();
        }

    }
}

